layui.extend({
    ztree: 'zTree_v3/ztree' 
  }).define(['layer', 'ztree', 'admin'], function(exports){

    var $ = layui.$
    ,admin = layui.admin;
    var operate = {
        treeShow: function(obj){
            // tree的id 为了获取数据 必填
            var id = $(obj).attr("_id");
            // url 必填
            var url = $(obj).attr("_href");
            // 必填title
            var title = $(obj).attr("_title");
            // type 为弹出显示还是 不弹出显示
            var type = $(obj).attr("_type");
            if (type ==undefined) {
              // 默认弹出显示 当type为current时为当前页面显示
              type = "";
            }
            var extId = $(obj).attr("_extId");
            if (extId ==undefined) {
              extId = "";
            }
            var isShowHide = $(obj).attr("_isShowHide");
            if (isShowHide ==undefined) {
              isShowHide = "";
            }
            // 需要展示的树类型
            var showCheckbox = $(obj).attr("_checkbox");
            if (showCheckbox ==undefined) {
              showCheckbox = "";
            }
             // 需要展示的树类型
             var width = $(obj).attr("_width");
             if (width ==undefined) {
               width = "420px";
             }
              // 需要展示的树类型
            var height = $(obj).attr("_height");
            if (height ==undefined) {
              height = "420px";
            }
            var setting = {
              check:{
                
                chkboxType:{

                }
              },
              data : {   
                  simpleData : {
                      enable : true, // 设置启用简单数据格式[{id, pId, name}, {id, pId, name}]   
                      idKey : "id",  // 节点数据中保存唯一标识的属性名称
                      pIdKey : "pId",  // 节点数据中保存其父节点唯一标识的属性名称
                      rootPId : null // 根节点id
                  }
              }
            };
            
            // 接收到的参数
            var style = {
              id: id,
              url: url,
              title:title,
              type: type,
              extId: extId,
              isShowHide: isShowHide,
              showCheckbox: showCheckbox, // radio 或者checkbox
              width: width,
              height: height
            }
        
            admin.req({
                type:"GET",
                url: url+"?extId="+extId+"&isShowHide="+isShowHide,     
                dataType:"json",
                success:function(res){
                  chealTree(style, setting, res.data);
                },
                error:function(jqXHR){
                    alert("发生错误："+ jqXHR.status);
                }
            });
        }
      };
        

      // type 为弹出显示还是 不弹出显示
    function chealTree(style, setting, data) {
        if (style.checkbox == "checkbox") {
            setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
            setting.check.enable = true;
        } else {
          setting.check.enable = true;
          setting.check.chkStyle = "radio";
          setting.check.radioType= "all";
        }
        var treeObj = layui.ztree.init($("#"+style.id), setting, data);
        if(style.type == "current") {
          $("#"+style.id).parent().show();
        } else {
          var index = layer.open({
            type: 1
            ,title: style.title
            ,content: $("#"+style.id).parent()
            ,area: [style.width, style.height]
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
              var nodes=treeObj.getCheckedNodes(true);
              if(nodes.length > 0) {
                window.tools.saveTreeData(nodes);
                layer.close(index);
              } else {
                layer.msg("请至少选择一个节点!");
              }
            }
          }); 
        }
        
    }

    exports('ztreeUtil', operate );
});